import {
	IAbility,
	IActionResult,
	IContext,
	IDECtrlAbility,
	IGlobalActionHelper,
	IPanelDetailAbility,
	IParam,
	IViewAbility,
} from '@/core';
import { hasFunction } from '@/core/utils';
import { router } from '@/router';

/**
 * 全局界面行为辅助类
 */
export class GlobalActionHelper implements IGlobalActionHelper {
	/**
	 * 单例服务
	 *
	 * @protected
	 * @static
	 * @type {IGlobalActionHelper}
	 * @memberof GlobalActionHelper
	 */
	private static service: IGlobalActionHelper;

	/**
	 * 获取实例
	 *
	 * @static
	 * @return {*}  {IGlobalActionHelper}
	 * @memberof GlobalActionHelper
	 */
	static getInstance(): IGlobalActionHelper {
		if (!this.service) {
			this.service = new GlobalActionHelper();
		}
		return this.service;
	}

	/**
	 * 预置行为predefinedType与tag映射表
	 *
	 * @private
	 * @type {Map<string, string>}
	 * @memberof GlobalActionHelper
	 */
	private predefinedActionMap: Map<string, string> = new Map([
		['EDITVIEW_EXITACTION', 'Exit'],
		['EDITVIEW_SAVEANDEXITACTION', 'SaveAndExit'],
		['TREEVIEW_REFRESHPARENTACTION', 'RefreshParent'],
		['GRIDVIEW_EXPORTXMLACTION', 'ExportModel'],
		['GRIDVIEW_EXPORTACTION', 'ExportExcel'],
		['EDITVIEW_REMOVEANDEXITACTION', 'RemoveAndExit'],
		['GRIDVIEW_PRINTACTION', 'Print'],
		['EDITVIEW_NEXTRECORDACTION', 'NextRecord'],
		['GRIDVIEW_NEWROWACTION', 'NewRow'],
		['EDITVIEW_LASTRECORDACTION', 'LastRecord'],
		['EDITVIEW_PREVRECORDACTION', 'PrevRecord'],
		['GRIDVIEW_SEARCHBAR', 'ToggleFilter'],
		['EDITVIEW_SAVEANDSTARTWFACTION', 'SaveAndStart'],
		['EDITVIEW_NEWACTION', 'New'],
		['EDITVIEW_PRINTACTION', 'Print'],
		['EDITVIEW_COPYACTION', 'Copy'],
		['EDITVIEW_HELPACTION', 'Help'],
		['EDITVIEW_FIRSTRECORDACTION', 'FirstRecord'],
		['GRIDVIEW_REFRESHACTION', 'Refresh'],
		['EDITVIEW_SAVEANDNEWACTION', 'SaveAndNew'],
		['EDITVIEW_VIEWWFSTEPACTORACTION', 'ViewWFStep'],
		['EDITVIEW_SAVEACTION', 'Save'],
		['TREEVIEW_REFRESHALLACTION', 'RefreshAll'],
		['GRIDVIEW_IMPORTBAR', 'Import'],
		['GRIDVIEW_ROWEDITACTION', 'ToggleRowEdit'],
		['GRIDVIEW_NEWACTION', 'New'],
		['GRIDVIEW_EDITACTION', 'Edit'],
		['GRIDVIEW_HELPACTION', 'Help'],
		['EDITVIEW_REFRESHACTION', 'Refresh'],
		['GRIDVIEW_REMOVEACTION', 'Remove'],
		['GRIDVIEW_COPYACTION', 'Copy'],
		['GRIDVIEW_VIEWACTION', 'View'],
		['GRIDVIEW_SAVEROWACTION', 'SaveRow'],
		['APP_LOGIN', 'Login'],
		['APP_LOGOUT', 'Logout'],
		['DATA_CREATEOBJECT', 'DataCreate'],
		['DATA_REMOVEOBJECT', 'DataRemove'],
		['DATA_SAVECHANGES', 'DataSaveChange'],
		['DATA_CANCELCHANGES', 'DataCancelChange'],
		['DATA_SYNCHRONIZE', 'DataSynchronize'],
	]);

	/**
	 * 通过传入tag获取行为方法名称（兼容老写法，识别新增属性predefinedType）
	 *
	 * @private
	 * @param {string} predefinedType
	 * @return {*}  {string}
	 * @memberof GlobalActionHelper
	 */
	private getActionMethodByTag(predefinedType: string): string {
		if (this.predefinedActionMap.get(predefinedType)) {
			return this.predefinedActionMap.get(predefinedType) as string;
		} else {
			return predefinedType;
		}
	}

	/**
	 * 执行全局界面行为
	 *
	 * @template T 能力泛型
	 * @param {string} predefinedType 预置行为类型
	 * @param {IContext} context 应用上下文
	 * @param {IParam} viewParams 视图参数
	 * @param {IParam[]} data 数据
	 * @param {MouseEvent} event 鼠标源事件
	 * @param {T} ability 能力
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async executeUIAction<T extends IAbility>(
		predefinedType: string,
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		const that: any = this;
		const actionTag = this.getActionMethodByTag(predefinedType);
		if (that[actionTag] && that[actionTag] instanceof Function) {
			return await that[actionTag](context, viewParams, data, event, ability);
		} else {
			console.warn(`${actionTag} 暂未实现`);
			return { ok: false, data: null };
		}
	}

	/**
	 * 帮助
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Help<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('帮助暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 保存
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Save<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'save')) {
			return { ok: false, data: null };
		}
		const result: IActionResult = await (ability as any).save(viewParams);
		return result;
	}

	/**
	 * 退出
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Exit<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'closeView')) {
			return { ok: false, data: null };
		}
		return (ability as any).closeView();
	}

	/**
	 * 保存并退出
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async SaveAndExit<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		const result: IActionResult = await this.Save(
			context,
			viewParams,
			data,
			event,
			ability
		);
		if (result.ok) {
			return (ability as any).closeView();
		} else {
			return { ok: false, data: null };
		}
	}

	/**
	 * 保存并新建
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async SaveAndNew<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('保存并新建暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 保存行
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async SaveRow<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'save')) {
			return { ok: false, data: null };
		}
		const result: IActionResult = await (ability as any).save(viewParams);
		return result;
	}

	/**
	 * 查看
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async View<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'openData')) {
			return { ok: true, data: null };
		}
		(ability as any).openData(context, viewParams, data);
		return { ok: true, data: null };
	}

	/**
	 * 打印
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Print<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('打印暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 当前流程步骤
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async ViewWFStep<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('打印暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 导出
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async ExportExcel<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'exportExcel')) {
			return { ok: true, data: null };
		}
		App.getNotificationHelper().warning('导出暂未支持');
		return { ok: true, data: null };
	}

	/**
	 * 第一条记录
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async FirstRecord<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('第一条记录暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 最后一条记录
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async LastRecord<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('最后一条记录暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 前一条记录
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async PrevRecord<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('前一条记录暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 后一条记录
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async NextRecord<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('后一条记录暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 切换过滤器
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async ToggleFilter<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'toggleFilter')) {
			return { ok: true, data: null };
		}
		// (ability as unknown as IMDViewAbility).toggleFilter();
		App.getNotificationHelper().warning('切换过滤器暂未支持');
		return { ok: true, data: null };
	}

	/**
	 * 开始流程
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async SaveAndStart<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('开始流程暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 拷贝
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Copy<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		App.getNotificationHelper().warning('拷贝暂未支持');
		return { ok: false, data: null };
	}

	/**
	 * 删除
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Remove<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'remove')) {
			return { ok: false, data: null };
		}
		const result: IActionResult = await (ability as any).remove(data);
		return result;
	}

	/**
	 * 删除并关闭
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async RemoveAndExit<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		const result: IActionResult = await this.Remove(
			context,
			viewParams,
			data,
			event,
			ability
		);
		if (result.ok) {
			return (ability as any).closeView();
		} else {
			return { ok: false, data: null };
		}
	}

	/**
	 * 刷新
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Refresh<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'refresh')) {
			return { ok: true, data: null };
		}
		const result: IActionResult = await (ability as any).refresh(viewParams);
		return result;
	}

	/**
	 * 刷新父
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async RefreshParent<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'refreshParentNode')) {
			return { ok: true, data: null };
		}
		const result: IActionResult = await (ability as any).refreshParentNode();
		return result;
	}

	/**
	 * 刷新全部
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async RefreshAll<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'refreshAllNode')) {
			return { ok: true, data: null };
		}
		const result: IActionResult = await (ability as any).refreshAllNode();
		return result;
	}

	/**
	 * @description 数据导出
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Import<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'importExcel')) {
			return { ok: true, data: null };
		}
		App.getNotificationHelper().warning('数据导出暂未支持');
		return { ok: true, data: null };
	}

	/**
	 * 新建
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async New<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'newData')) {
			return { ok: true, data: null };
		}
		(ability as unknown as IDECtrlAbility).newData(context, viewParams, data);
		return { ok: true, data: null };
	}

	/**
	 * 编辑
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Edit<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'openData')) {
			return { ok: true, data: null };
		}
		(ability as unknown as IDECtrlAbility).openData(context, viewParams, data);
		return { ok: true, data: null };
	}

	/**
	 * 新建行
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async NewRow<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'newRow')) {
			return { ok: true, data: null };
		}
		App.getNotificationHelper().warning('新建行暂未支持');
		return { ok: true, data: null };
	}

	/**
	 * 行编辑
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async ToggleRowEdit<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		if (!ability || !hasFunction(ability, 'toggleRowEdit')) {
			return { ok: true, data: null };
		}
		App.getNotificationHelper().warning('行编辑暂未支持');
		return { ok: true, data: null };
	}

	/**
	 * 登录
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Login<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: IViewAbility
	): Promise<void> {
		// const layoutData = data.length > 0 ? data[0] : {};
		// const layoutModelDetails: any = ability.getLayoutModelDetails() || {};
		// const handlePanelValueChange: Function = ability.handlePanelValueChange;
		// let userNameKey: string = Object.keys(layoutModelDetails).find((key: string) => Object.is(layoutModelDetails[key].predefinedType, 'AUTH_USERID')) || '';
		// let passwordKey: string = Object.keys(layoutModelDetails).find((key: string) => Object.is(layoutModelDetails[key].predefinedType, 'AUTH_PASSWORD')) || '';
		// let messageKey: string = Object.keys(layoutModelDetails).find((key: string) => Object.is(layoutModelDetails[key].predefinedType, 'AUTH_LOGINMSG')) || '';
		// if (!userNameKey || !passwordKey) {
		//     if (messageKey) {
		//         handlePanelValueChange( messageKey, '用户名或密码未配置！');
		//     } else {
		//         App.getNotificationHelper().warning('用户名或密码未配置！');
		//     }
		// }
		// const loginname: any = layoutData[userNameKey];
		// const password: any = layoutData[passwordKey];
		// if (!loginname || !password) {
		//     if (messageKey) {
		//         handlePanelValueChange( messageKey, '请输入用户名与密码');
		//     } else {
		//         App.getNotificationHelper().warning('请输入用户名与密码');
		//     }
		// }
		// App.login(loginname, password);
	}

	/**
	 * 登出
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async Logout<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<void> {
		App.logout().then(() => {
			router.push({
				path: '/login',
				query: { redirect: window.location.hash.replace('#', '') },
			});
		});
	}

	/**
	 * 建立数据
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async DataCreate<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		let result: IActionResult = { ok: true, data: null };
		if (!ability) {
			return result;
		}
		if (hasFunction(ability, 'createData')) {
			// await (ability as unknown as IPanelDetailAbility).createData(context, viewParams, data);
			// todo
		}
		return result;
	}

	/**
	 * 删除数据
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async DataRemove<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		let result: IActionResult = { ok: true, data: null };
		if (!ability) {
			return result;
		}
		if (hasFunction(ability, 'removeData')) {
			// await (ability as unknown as IPanelDetailAbility).removeData(context, viewParams, data);
			// todo
		} else {
			result = await this.Remove(context, viewParams, data, event, ability);
		}
		return result;
	}

	/**
	 * 保存变更
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async DataSaveChange<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		let result: IActionResult = { ok: true, data: null };
		if (!ability) {
			return result;
		}
		if (hasFunction(ability, 'saveData')) {
			await (ability as unknown as IPanelDetailAbility).saveData(
				context,
				viewParams,
				data
			);
		} else {
			result = await this.Save(context, viewParams, data, event, ability);
		}
		return result;
	}

	/**
	 * 取消变更
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async DataCancelChange<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: IViewAbility
	): Promise<void> {
		const model = ability.getModel();
		if (model.viewType && model.viewType === 'APPLOGINVIEW') {
			// const layoutData = data.length > 0 ? data[0] : {};
			// const layoutModelDetails: any = ability.getLayoutModelDetails() || {};
			// const handlePanelValueChange: Function = ability.handlePanelValueChange;
			// const length = Object.keys(layoutData).length;
			// for (let i = length - 1; i >= 0; i--) {
			//     const name = Object.keys(layoutData)[i];
			//     if (layoutData.hasOwnProperty(name) && !Object.is(layoutModelDetails[name].itemType, 'CONTAINER')) {
			//         handlePanelValueChange( name, null );
			//     }
			// }
		} else {
			if (ability.closeView && ability.closeView instanceof Function) {
				ability.closeView();
			}
		}
	}

	/**
	 * 数据同步
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof GlobalActionHelper
	 */
	public async DataSynchronize<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T
	): Promise<IActionResult> {
		let result: IActionResult = { ok: true, data: null };
		if (
			!ability ||
			(!hasFunction(ability, 'refresh') &&
				!hasFunction(ability, 'refreshDataArea'))
		) {
			return result;
		}
		if (hasFunction(ability, 'refresh')) {
			result = await (ability as any).refresh(viewParams);
		}
		if (hasFunction(ability, 'refreshDataArea')) {
			// result = await (ability as unknown as IPanelDetailAbility).refreshDataArea(viewParams);
			// todo
		}
		return result;
	}
}
